﻿/****************************** Module Header ******************************\
 * Module Name:  _Cookies.cs
 * Module Version:  1.0.0 (2012-9-16)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for _Request.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UCF.Core.Web
{
    public class _Cookies
    {
        public static HttpCookieCollection Cookies { get { return HttpContext.Current.Request.Cookies; } }
        
        public static implicit operator _Cookies(HttpCookie cookie)
        {
            return new _Cookies(cookie);
        }
        static HttpCookie instance;
        public _Cookies(HttpCookie cookie)
        {
            instance=cookie;
        }
        public static _Cookies Get(string name)
        {
            instance = HttpContext.Current.Request.Cookies[name];
            if (instance != null) return instance;
            instance = new HttpCookie(name);
            HttpContext.Current.Response.Cookies.Add(instance);
            return instance;
        }
        public string GetPara(string name)
        {
            return instance.Value.GetParameterValue(name);
        }

        public void SetValue(string v)
        {
            instance.Value = v;
            HttpContext.Current.Response.SetCookie(instance);
        }
        public string GetValue()
        {
            return instance == null ? string.Empty : instance.Value;
        }

        public void Clear()
        {
            HttpContext.Current.Response.Cookies[instance.Name].Value = string.Empty;
            HttpContext.Current.Response.Cookies[instance.Name].Expires = DateTime.Now.AddDays(-1);
        }
    }
}